package com.youle.askdao.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

/**
 * Created by XiaoGe on 2018/10/12.
 */
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {
//    //本地文件地址
//    @Value("${file.winupload}")
//    private  String fileDirWin;
//    //服务器文件地址
//    @Value("${file.centosupload}")
//    private String fileDirCentos;
//
//    @Value("${file.cardsfail}")
//    private String cardsfailPath;

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

    /**放行静态资源*/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//        .addResourceLocations("file:"+fileDirWin).addResourceLocations("file:"+fileDirCentos);

        registry.addResourceHandler("index.html").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //favicon.ico
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/resources/");

        super.addResourceHandlers(registry);
    }

    /**
     * 解决跨域问题，开放所有方法
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
                .allowCredentials(false).maxAge(60*60*2);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器，添加拦截路径和排除拦截路径
        /*registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/user/dologin","/index.html","/lock","/");*/
        //角色权限拦截器
//        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**")
//                .excludePathPatterns("/user/dologin","/index.html","/lock","/");
    }

}
